package com.aduerr.v1shouyin.util;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import com.aduerr.v1shouyin.App;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * Created by fyf on 2018/3/30 0030.
 */

public class Tools {


    private static List<Activity> list;
    /**
     * 7
     * 防止快速点击
     *
     * @return
     */
    private static long lastClickTime;

    /**
     * 1.0
     * 跳转界面
     *
     * @param aClass
     */
    public static void startActivity(Context context, Class aClass) {
        context.startActivity(new Intent(context, aClass));
    }

    /**
     * 1.1
     * 跳转界面
     *
     * @param activity
     */
    public static void startActivity(Context context, Activity activity) {
        context.startActivity(new Intent(context, activity.getClass()));
    }

    /**
     * 2
     * 添加activity
     *
     * @param activity
     */
    public static void addActivity(Activity activity) {
        if(list == null)
            list = new ArrayList<>();
        if(list.contains(activity)) return;
        list.add(activity);
    }

    /**
     * 3
     * 结束所有activity
     */
    public static void finishAll() {
        if(list == null) return;
        if(list.size() == 0) return;
        for(Activity a : list) {
            a.finish();
        }
    }

    public synchronized static boolean isFastClick(int fastTime) {
        long time = System.currentTimeMillis();
        if(time - lastClickTime < fastTime) {
            return true;
        }
        lastClickTime = time;
        return false;
    }


    /**
     * dp转换px
     *
     * @param context
     * @param dp
     * @return
     */
    public static int Dp2Px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    /**
     * model 是否可用
     *
     * @param m
     * @return
     */
//    public static boolean isAvailable(IEntity m) {
//        if (m == null) return true;
//        if (m.getSuccess() == -1 || m.getSuccess() == -2 || m.getSuccess() == -6 || m
//        .getSuccess() == -3 || m.getSuccess() == -300) {//状态过期 重新登录
//            Intent intent = new Intent(App.getApp(), LoginActivity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            ToastUtils.showToast(App.getApp(), m.getErrMsg());
//            App.getApp().startActivity(intent);
//            exitApp();
//            return true;
//        }
//        if (m.getSuccess() == -100 || m.getSuccess() == -200) {
//            Intent intent = new Intent(App.getApp(), BangGuidActivity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            ToastUtils.showToast(App.getApp(), m.getErrMsg());
//            App.getApp().startActivity(intent);
//        }
//        return false;
//    }

    /**
     * 退出APP
     */
    public static void exitApp() {
        SharedPreferencesUtils.remove(App.getApp(), "autologin");
        SharedPreferencesUtils.remove(App.getApp(), "remberpassword");
    }


    /**
     * @param s 判断是否有空值
     * @return
     */
    public static boolean isNull(String... s) {
        for(String value : s) {
            if("".equals(value)) return true;
        }
        return false;
    }

//    /**
//     * 设置圆形头像
//     *
//     * @param imgUrl
//     * @param iv
//     */
//    public static void setCircleIcon(String imgUrl, ImageView iv) {
//        Glide.with(App.getApp()).asBitmap().apply(RequestOptions.bitmapTransform(new
//        CircleCrop()).
//                error(R.drawable.ic_default_head).diskCacheStrategy(DiskCacheStrategy
//                .ALL)
//                .placeholder(R.drawable.ic_default_head)).load(imgUrl).into(iv)
//                .onStart();
//    }

    /**
     * 通用加载图片
     *
     * @param imgUrl
     * @param iv
     */
//    public static void setLoadingPhoto(String imgUrl, ImageView iv) {
//        Glide.with(App.getApp()).asBitmap().apply(RequestOptions.errorOf(R.drawable
//        .ic_default_head)).load(imgUrl).into(iv).onStart();
//    }

    /**
     * 隐藏软键盘
     *
     * @param activity
     */
    public static void setInputMethod(Activity activity) {
        View view = activity.getWindow().peekDecorView();
        if(view != null) {
            InputMethodManager inputmanger =
                    (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public static int getVersion() {
        try {
            PackageManager manager = App.getApp().getPackageManager();
            PackageInfo info = manager.getPackageInfo(App.getApp().getPackageName(), 0);
            int version = info.versionCode;
            return version;
        } catch(Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    /**
     * 获取版本名
     *
     * @return当前应用的版本名
     */
    public static String getVersionName() {
        try {
            PackageManager manager = App.getApp().getPackageManager();
            PackageInfo info = manager.getPackageInfo(App.getApp().getPackageName(), 0);
            String versionName = info.versionName;
            return versionName;
        } catch(Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 四舍五入保留两位
     *
     * @param money
     * @return
     */
    public static String totalMoney(double money) {
        java.math.BigDecimal bigDec = new java.math.BigDecimal(money);
        double total = bigDec.setScale(2, java.math.BigDecimal.ROUND_HALF_UP)
                .doubleValue();
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(total);
    }

    public static String stampToDate(long timeMillis) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(timeMillis);
        return simpleDateFormat.format(date);
    }

    /**
     * 判断是否安装了支付包客户端
     *
     * @return
     */
    public static boolean checkAliPayInstalled() {

        Uri uri = Uri.parse("alipays://platformapi/startApp");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        ComponentName componentName =
                intent.resolveActivity(App.getApp().getPackageManager());
        return componentName != null;
    }


//    public static void setReceiver(String receiver, TextView tvReceiver) {
//        String szReceiver = MyApplication.getInstance().getString(R.string
//        .accountname, receiver);
//        SpannableStringBuilder ssb = new SpannableStringBuilder(szReceiver);
//        ssb.setSpan(new ForegroundColorSpan(MyApplication.getInstance().getResources
//        ().getColor(R.color.light_blue)), ssb.length() - receiver.length(), ssb
//        .length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//        if (!TextUtils.isEmpty(receiver)) {
//            tvReceiver.setText(ssb);
//        } else {
//            tvReceiver.setText(szReceiver);
//        }
//    }

}
